package com.dss.auto_deploy.util;


import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.api.model.Image;
import com.github.dockerjava.api.model.Info;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @Description:
 * @author: dongshoushan
 * @date: 2022年03月23日 11:09
 */
public class Test {
    public static void main(String[] args) {
        String yearMonthValue = "202211";
        String statPeriod = yearMonthValue.substring(0, 4) + "-" + yearMonthValue.substring(4, 6);
        System.out.println(statPeriod);

        DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder().withDockerTlsVerify(false)
                .withDockerHost("tcp://192.168.29.183:4243")
                .withRegistryEmail("dongss@lenovo.com").build();
        DockerClient dockerClient = DockerClientBuilder.getInstance(config).build();

        Info info = dockerClient.infoCmd().exec();
        System.out.println(JacksonUtils.toString(info));
        List<Image> exec = dockerClient.listImagesCmd().exec();
        List<Image> list =new ArrayList<>();
        for (Image image : exec) {
            String[] repoTags = image.getRepoTags();
            if (repoTags != null) {
//                Arrays.stream(repoTags).filter(repoTag -> repoTag.contains("<none>")).map(repoTag -> image).forEach(imageList::add);
            }
        }
        list.sort(Comparator.comparing(Image ::getCreated));

//        List<Image> imageList = dockerClient.listImagesCmd().exec().stream().sorted(Comparator.comparing(Image::getCreated)).collect(Collectors.toList());
        System.out.println(JacksonUtils.toString(list));

//
//        List<Container> containerList = dockerClient.listContainersCmd().withShowAll(true).exec();
//        for (Container container : containerList) {
//            String[] names = container.getNames();
//            String collect = Arrays.stream(names).collect(Collectors.joining(","));
//            System.out.println("collect:"+collect);
//            System.out.println(container.getImage());
//        }
//        System.out.println(JacksonUtils.toString(containerList));
//        //停止、删除容器
//        delContainer(dockerClient,"lenovo-test");



    }

    private static void delContainer(DockerClient dockerClient, String s) {
        List<Container> filterList = dockerClient.listContainersCmd().withNameFilter(Arrays.asList(s)).exec();
        System.out.println(JacksonUtils.toString(filterList));
        for (Container container : filterList) {
            dockerClient.stopContainerCmd(container.getId()).exec();
            System.out.println("停止容器：" + container.getNames()[0]);
            dockerClient.removeContainerCmd(container.getId()).withForce(true).withRemoveVolumes(true).exec();
            System.out.println("删除容器：" + container.getNames()[0]);
        }
    }

    /**
     * 创建容器
     *
     * @param dockerClient
     */
    public static CreateContainerResponse createContainer(DockerClient dockerClient, String containerName, String imageName,
                                                          HostConfig hostConfig, List<String> envs) {
        CreateContainerResponse container = dockerClient.createContainerCmd(imageName)
                .withName(containerName)
                .withHostConfig(hostConfig)
                .withCmd()
                .withEnv(envs)
                .exec();
        return container;
    }
}
